﻿
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Resources;
using System.Reflection;
using System.Windows.Forms.DataVisualization.Charting;
using System.Threading;

namespace Adaptive_Clustering
{
     public partial class MainGUI : Form
     {

        MainController MainCTRL;
        ResultImage[] ResultImages;
        Color MarkColor = new Color();

        public MainGUI(MainController MainCTRL) //constructor
        {       
            this.MainCTRL = MainCTRL;
            InitializeComponent();
   
        }

        private void Main_Load(object sender, EventArgs e)
        {
            ResultChart.Series.Clear(); 
            ResultChart.Series.Add("Silhouette");
            ResultChart.Series["Silhouette"].ChartType = SeriesChartType.Line;
            ResultChart.Series["Silhouette"].MarkerStyle = MarkerStyle.Cross; 
            ResultChart.Series["Silhouette"].MarkerSize = 20;         
            ResultChart.Series["Silhouette"].Color = Color.Blue;
            ResultChart.ChartAreas[0].AxisY.Maximum = 1;
            ResultChart.ChartAreas[0].AxisY.Minimum = -1;
        
            MarkColor = Color.FromArgb(255,0,0);
        }

        private void Select_Image_BTN_Click(object sender, EventArgs e)
        {
            
            OpenFileDialog NewPic = new OpenFileDialog();
            NewPic.Filter = "Image Files(*.BMP;*.JPG;*.JPEG;*.GIF;*.PNG)|*.BMP;*.JPG;*.JPEG;*.GIF;*.PNG";
            if (NewPic.ShowDialog() == DialogResult.OK)
            {
                MainCTRL.ResetImage();
                ResultChart.Series["Silhouette"].Points.Clear();
                MainCTRL.LoadImage(NewPic.FileName);
                this.Result_Image_LBL.Visible = false; 
                this.Chart_PNL.Visible = false;
                this.Results_PNL.Visible = true;
                this.AllResults_CMBox.Visible = false;
            }
        }

        public int MinKBox
        {
            get
            {
                if (MinK_Box.Value < Constants.K_MinVal)
                    MinK_Box.Value = Constants.K_MinVal;
                else if (MinK_Box.Value > Constants.K_MaxVal)
                    MinK_Box.Value = Constants.K_MaxVal;
                return (int)MinK_Box.Value;
            }
            set
            {
                if (MinK_Box.Value < Constants.K_MinVal)
                    MinK_Box.Value = Constants.K_MinVal;
                else if ((MinK_Box.Value > Constants.K_MaxVal) || (MinK_Box.Value > K_box.Value))
                    MinK_Box.Value = K_box.Value;
                     else
                        MinK_Box.Value = value;
            }
        }

        public int KBox
        {
            get
            {
                if (K_box.Value < Constants.K_MinVal)
                    K_box.Value = Constants.K_MinVal;
                else if (K_box.Value > Constants.K_MaxVal)
                    K_box.Value = Constants.K_MaxVal;
                return (int)K_box.Value;
            }
            set
            {
                if ((K_box.Value < Constants.K_MinVal) || (K_box.Value < MinK_Box.Value))
                    K_box.Value = MinK_Box.Value;
                else if (K_box.Value > Constants.K_MaxVal)
                    K_box.Value = Constants.K_MaxVal;
                     else
                        K_box.Value = value;
            }
        }

        public int SigmaBox
        {
            get
            {
                return (int)Sigma_Box.Value;
            }
            set
            {
                Sigma_Box.Value = value;
            }
        }

        public int BetaBox
        {
            get
            {
                return (int)Beta_Box.Value;
            }
            set
            {
                Beta_Box.Value = value;
            }
        }

        public int ProcessImageProgressBar
        {
            get
            {
                return Process_Image_Progress_Bar.Value;
            }
            set
            {
                Process_Image_Progress_Bar.Value = value;
            }
        }

        public bool ColorCheckBox
        {
            get
            {
                return Color_CheckBox.Checked;
            }
        }
         public bool EnableBilinear
        {
             get
            {
                return BilinearFunc_CHKBOX.Checked;
            }
        }


     private void Reset_Selection_BTN_Click(object sender, EventArgs e)
     {
         MinKBox = 2;
         KBox = 4;
         SigmaBox = 2;
         BetaBox = 1;
         MainCTRL.ResetImage();
         ResultChart.Series["Silhouette"].Points.Clear();
         this.Result_Image_LBL.Visible = false;
         Main_Control_PNL.Visible = true;
         this.Chart_PNL.Visible = false;
         this.Results_PNL.Visible = true;
         this.AllResults_CMBox.Visible = false;
     }

     private void Process_Image_BTN_Click(object sender, EventArgs e)
     {
         if (MainCTRL.ImageIsLoaded())
         {
             ResultChart.Series["Silhouette"].Points.Clear();
             Main_Control_PNL.Enabled = false;
             Process_Image_Progress_Bar.Value = 0;
             Process_Image_Progress_Bar.Visible = true;
             
             ResultImages = MainCTRL.ProccessImage(KBox, MinKBox, SigmaBox, BetaBox, ColorCheckBox, MarkColor, EnableBilinear);
             
             this.AllResults_CMBox.Items.Clear();
             
             for (int kCouter = MinKBox; kCouter <= KBox; kCouter++)
             {
                 ResultChart.Series["Silhouette"].Points.AddXY(kCouter, ResultImages[kCouter].GetBestSxIndexValue());
                 this.AllResults_CMBox.Items.Add(kCouter.ToString());
             }
             this.AllResults_CMBox.SelectedIndex = MainCTRL.GetBestKindex() - MinKBox;
             this.AllResults_CMBox.Visible = true;
             this.Result_Image_LBL.Visible = true;
             Process_Image_Progress_Bar.Value = 0;
             Process_Image_Progress_Bar.Visible = false;
             Main_Control_PNL.Enabled = true;
         }
     }

     private void Loadmenu_Click(object sender, EventArgs e)
     {
         Select_Image_BTN_Click(this, new EventArgs());
     }

     private void Quitmenu_Click(object sender, EventArgs e)
     {
         QuitButton_Click(this, new EventArgs());
     }

     private void Savemenu_Click(object sender, EventArgs e)
     {
         SaveFileDialog NewPic = new SaveFileDialog();
         NewPic.Filter ="Bitmap Image (.bmp)|*.bmp|Gif Image (.gif)|*.gif|JPEG Image (.jpeg)|*.jpeg|Png Image (.png)|*.png|Tiff Image (.tiff)|*.tiff|Wmf Image (.wmf)|*.wmf";
         if (MainCTRL.ImageIsProccessed())
         {
             if (NewPic.ShowDialog() == DialogResult.OK)
                 MainCTRL.SaveImage(NewPic.FileName);
         }
         else //if can't save, display Error
             MessageBox.Show("Image save operation Failed.", "Error!", MessageBoxButtons.OK);
     }
     
     private void resetProgram_Click(object sender, EventArgs e)
     {
        Reset_Selection_BTN_Click(this, new EventArgs());
     }

     private void QuitButton_Click(object sender, EventArgs e)
     {
         if (MessageBox.Show("Are you sure you want to quit ?", "Exit", MessageBoxButtons.OKCancel) == DialogResult.OK)
             Application.Exit();
     }

     public void UpadteImage(Bitmap image)
     {
         Result_Image_PctBx.SizeMode = PictureBoxSizeMode.StretchImage;
         Result_Image_PctBx.Image = image;   
     }

     private void Color_CheckBox_CheckedChanged(object sender, EventArgs e)
     {    
         switch (Color_CheckBox.CheckState)
         {
             case CheckState.Checked:
                 Color_CheckBox.Image = (Image)Properties.Resources.checkbox_checked;
                 break;
             case CheckState.Unchecked:
                Color_CheckBox.Image = (Image)Properties.Resources.checkbox_empty;
                 break;
         } 
     }
     private void BilinearFunc_CHKBOX_CheckedChanged(object sender, EventArgs e)
     {
         switch (BilinearFunc_CHKBOX.CheckState)
         {
             case CheckState.Checked:
                 BilinearFunc_CHKBOX.Image = (Image)Properties.Resources.checkbox_checked;
                 break;
             case CheckState.Unchecked:
                 BilinearFunc_CHKBOX.Image = (Image)Properties.Resources.checkbox_empty;
                 break;
         }

     }

     private void MinK_Box_ValueChanged(object sender, EventArgs e)
     {
         MinKBox = (int)MinK_Box.Value;
     }

     private void K_box_ValueChanged(object sender, EventArgs e)
     {
         KBox = (int)K_box.Value;
     }

     private void ShowChartBTN_Click(object sender, EventArgs e)
     {
         this.Results_PNL.Visible = false;
         this.Chart_PNL.Visible = true;
     }

     private void ShowResultsBTN_Click(object sender, EventArgs e)
     {
         this.Chart_PNL.Visible = false;
         this.Results_PNL.Visible = true;
     }

     private void AllResults_CMBox_SelectedIndexChanged(object sender, EventArgs e)
     {
         string S_k=this.AllResults_CMBox.GetItemText(this.AllResults_CMBox.SelectedItem);
         int k = Convert.ToInt32(S_k);
         MainCTRL.DisplayImage(ResultImages[k].GetImageArray(), ResultImages[k].GetImageClusterIndexArray(), ColorCheckBox, k, ResultImages[k].GetBestSxIndex(), MarkColor);
     }

     private void formColor_Click(object sender, EventArgs e)
     {
         if (colorDialog1.ShowDialog() == DialogResult.OK)
         {
             MarkColor = colorDialog1.Color;
         }
     }
    }
}
